﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Homework_7
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.Write("请输入关键字,例如“高校电话号”："); string keyword = Console.ReadLine();

            List<string> urls = await Search(keyword);

            HashSet<string> phoneNumbers = new HashSet<string>();
            Dictionary<string, List<string>> urlPhoneMap = new Dictionary<string, List<string>>();

            foreach (string url in urls)
            {
                Console.WriteLine($"正在爬取 {url}...");

                string html = await Download(url);

                MatchCollection matches = Regex.Matches(html, @"\d{3,4}-\d{7,8}|\d{11}"); // 匹配电话号码的正则表达式
                //MatchCollection matches = Regex.Matches(html, @"^(\d{3,4}-)?\d{6,8}$");

                foreach (Match match in matches)
                {
                    string phoneNumber = match.Value;

                    if (!phoneNumbers.Contains(phoneNumber))
                    {
                        phoneNumbers.Add(phoneNumber);

                        if (!urlPhoneMap.ContainsKey(phoneNumber))
                        {
                            urlPhoneMap[phoneNumber] = new List<string>();
                        }

                        urlPhoneMap[phoneNumber].Add(url);
                    }
                }
            }

            Console.WriteLine("已爬取的URL：");
            foreach (string url in urls)
            {
                Console.WriteLine(url);
            }

            Console.WriteLine("找到的电话号码：");
            foreach (string phoneNumber in phoneNumbers)
            {
                Console.WriteLine($"{phoneNumber}：{string.Join(", ", urlPhoneMap[phoneNumber])}");
            }

            Console.ReadLine();
        }

        static async Task<List<string>> Search(string keyword)
        {
            List<string> urls = new List<string>();

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

                string baiduUrl = $"https://www.baidu.com/s?wd={keyword}";
                string bingUrl = $"https://www.bing.com/search?q={keyword}";

                Task<string> baiduTask = client.GetStringAsync(baiduUrl);
                Task<string> bingTask = client.GetStringAsync(bingUrl);

                await Task.WhenAll(baiduTask, bingTask);

                string baiduHtml = baiduTask.Result;
                string bingHtml = bingTask.Result;

                MatchCollection baiduMatches = Regex.Matches(baiduHtml, @"<a\s+href=""(https?://[^""]+)""");
                MatchCollection bingMatches = Regex.Matches(bingHtml, @"<a\s+href=""(https?://[^""]+)""");

                foreach (Match match in baiduMatches)
                {
                    urls.Add(match.Groups[1].Value);
                }

                foreach (Match match in bingMatches)
                {
                    urls.Add(match.Groups[1].Value);
                }
            }

            return urls;
        }

        static async Task<string> Download(string url)
        {
            //修改请求头，模拟浏览器访问，以避免被拒绝
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

                return await client.GetStringAsync(url);
            }
        }
    }
}